package com.kymatrix.kycarbon.xsmd.system.server.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.kymatrix.kycarbon.xsmd.system.server.model.entity.SystemConfigEntity;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.config.InsertSystemConfigReq;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.config.ListQuerySystemConfigReq;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.config.ListQuerySystemConfigResp;
import com.kymatrix.kycarbon.xsmd.system.server.model.form.config.UpdateSystemConfigReq;
import com.kymatrix.kycarbon.xsmd.system.server.service.ISystemConfigService;
import com.kymatrix.kycarbon.xsmd.common.business.common.ResultResp;
import com.kymatrix.kycarbon.xsmd.common.constants.Constant;
import com.kymatrix.kycarbon.xsmd.common.enums.EnableTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "系统配置相关请求")
@Slf4j
public class SystemConfigController {


    @Resource
    private ISystemConfigService systemConfigService;


    /**
     * 据分组名称查询配置列表
     */
    @ApiOperation("据分组名称查询配置列表")
    @PostMapping(value = "/sys/config/list")
    public ResultResp<ListQuerySystemConfigResp> list(@RequestBody @Validated ListQuerySystemConfigReq req) {
        ListQuerySystemConfigResp res = new ListQuerySystemConfigResp();
        List<SystemConfigEntity> entityList = systemConfigService.selectEnabledByGroupNames(req.getGroupNames());
        Map<String, List<SystemConfigEntity>> map = entityList.stream().collect(
                Collectors.groupingBy(SystemConfigEntity::getGroupName));
        for (Map.Entry<String, List<SystemConfigEntity>> entry : map.entrySet()) {
            List<ListQuerySystemConfigResp.SystemConfigInfo> list = new ArrayList<>();
            List<SystemConfigEntity> sortList = CollectionUtil.sort(
                    entry.getValue(), Comparator.comparingInt(SystemConfigEntity::getSortNo));
            for (SystemConfigEntity systemConfigEntity : sortList) {
                list.add(ListQuerySystemConfigResp.SystemConfigInfo.fromEntity(systemConfigEntity));
            }
            res.getMap().put(entry.getKey(), list);
        }
        return ResultResp.ok(res);
    }

    /**
     * 新增系统配置
     */
    @ApiOperation("新增系统配置")
    @PostMapping(value = "/sys/config/add")
    public ResultResp<String> add(@RequestBody @Validated InsertSystemConfigReq req) {
        SystemConfigEntity entity = new SystemConfigEntity();
        entity.setGroupName(req.getGroupName());
        entity.setConfigKey(req.getConfigKey());
        entity.setConfigValue(req.getConfigValue());
        entity.setEnableFlag(EnableTypeEnum.ENABLE.getCode());
        entity.setSortNo(req.getSortNo());
        this.systemConfigService.save(entity);
        return ResultResp.ok(entity.getId());
    }

    /**
     * 更新系统配置
     */
    @ApiOperation("更新系统配置")
    @PostMapping(value = "/sys/config/update")
    public ResultResp<String> add(@RequestBody @Validated UpdateSystemConfigReq req) {
        SystemConfigEntity entity = this.systemConfigService.getById(req.getId());
        entity.setGroupName(req.getGroupName());
        entity.setConfigKey(req.getConfigKey());
        entity.setConfigValue(req.getConfigValue());
        entity.setEnableFlag(req.getEnableFlag());
        entity.setSortNo(req.getSortNo());
        this.systemConfigService.save(entity);
        return ResultResp.ok(entity.getId());
    }

    /**
     * 删除系统配置
     */
    @ApiOperation("删除系统配置")
    @PostMapping(value = "/sys/config/delete")
    public ResultResp<String> delete(@RequestParam("id") String id) {
        SystemConfigEntity entity = this.systemConfigService.getById(id);
        if (entity == null) {
            return ResultResp.ok();
        }
        entity.setDeleted(Constant.DEL_FLAG_DELETE);
        this.systemConfigService.updateById(entity);
        return ResultResp.ok();
    }


}
